﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Application.Command.Add;
using Traceability.App.Domain;
using Traceability.App.ErrorCode;
using Traceability.App.Instructure;

namespace Traceability.App.Api.Application.Handler.Add
{
    /// <summary>
    /// 保存保健信息
    /// </summary>
    public class AddBaoHandler : IRequestHandler<AddBaoCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<HealthreportModel> healthreportRepo;
        private readonly IMapper mapper;

        public AddBaoHandler(IBaseRepository<HealthreportModel> healthreportRepo,IMapper mapper) 
        {
            this.healthreportRepo = healthreportRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(AddBaoCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var bao = healthreportRepo.GetAll().FirstOrDefault();
            var baos = mapper.Map<HealthreportModel>(request);
            baos.UpdateName = "张三";
            baos.UpdateTime = DateTime.Now;
            baos.CreateName = "张三";
            baos.CreateTime = DateTime.Now;
           
            var i = healthreportRepo.Add(baos);
            if (i>0)
            {
                res.Code = ApiEnum.Success;
                res.Data = i;
                res.Msg = "添加保健信息成功";
            }
            else
            {
                res.Code = ApiEnum.Error;
                res.Data = i;
                res.Msg = "添加保健信息失败";
            }
            return Task.FromResult(res);

        }
    }
}
